package com.uobgroup.ews.uam.core.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.uobgroup.ews.uam.core.domain.Profile;
import com.uobgroup.ews.uam.core.domain.Reference;
import com.uobgroup.ews.uam.core.domain.Segment;
import com.uobgroup.ews.uam.core.domain.UAMRole;

@Repository
public class ProfileDaoImpl extends AbstractJpaDao<Profile> implements ProfileDao {
   
   public ProfileDaoImpl() {
      setClazz(Profile.class);
   }
   
   @Override
   public long count() {
      return entityManager.createQuery("SELECT COUNT(o) FROM Profile o", Long.class).getSingleResult();
   }

   @Override
   public List<Profile> findAll(int firstResult, int maxResults) {
      return entityManager.createQuery("SELECT o FROM Profile o", Profile.class)
            .setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
   }

   @Override
   public List<Profile> findProfilesByUAMRole(UAMRole uamRole) {
      /*Query query = entityManager.createNamedQuery(Profile.QUERY_FIND_PROFILES_BY_ROLE);
      query.setParameter(Profile.PARAM_ROLE, uamRole);
      List<Profile> results = (List<Profile>)query.getResultList();*/
	   
	   //TODO : modify this method when needed 
      return new ArrayList<Profile>();
   }

   @Override
   public boolean isProfileInUse(Long id) {
      String queryStr = "select u from UAMUser u where u.profile.id = :id";
      Query query = entityManager.createQuery(queryStr);
      query.setParameter("id", id);
      return query.getResultList().size() > 0;
   }
   @Override
   public List<Profile> findAdminRole()
   {
		String queryStr = "select u from UAMRole u where u.isAdmin = 1";
		Query query = entityManager.createQuery(queryStr);
		List<UAMRole> list = query.getResultList(); 
		
		List<Profile> listProfile = new ArrayList<Profile>();
		
		for (UAMRole uamRole : list) {
			List<Profile> temp = uamRole.getProfiles();
			for (Profile profile : temp) {
				if(!listProfile.contains(profile))
					listProfile.add(profile);
			}
			
		}
		return listProfile;
				
		
   }
   @Override
   public List<Profile> findNonAdminRole()
   {
	   String queryStr = "select u from UAMRole u where u.isAdmin = 0";
		Query query = entityManager.createQuery(queryStr);
		List<UAMRole> list = query.getResultList(); 
		
		List<Profile> listProfile = new ArrayList<Profile>();
		
		for (UAMRole uamRole : list) {
			List<Profile> temp = uamRole.getProfiles();
			for (Profile profile : temp) {
				if(!listProfile.contains(profile))
					listProfile.add(profile);
			}
			
		}
		return listProfile;
   }

   /* 
    * Phuong Tran
    */
   @SuppressWarnings("unchecked")
   @Override
   public List<Profile> getProfileLstContainSegment(Segment seg) {
      String queryStr = "select p from Profile p join p.segments s where s.id = :segId";
      Query query = entityManager.createQuery(queryStr, Profile.class);
      query.setParameter("segId", seg.getSegmentReference().getId());
      return query.getResultList();
   }

   /**
    * @author dongn
    */
   @Override
   public long countProfilesThatHasSegmentReference(Reference segment) {
      String queryStr = "select COUNT(p) from Profile p join p.segments s where s.id = :segId";
      TypedQuery<Long> query = entityManager.createQuery(queryStr, Long.class);
      query.setParameter("segId", segment.getId());
      return query.getSingleResult(); 
   }
   
   
   
}
